package com.snail.snailvr.views.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.snail.snailvr.R;
import com.snail.snailvr.VRApp;
import com.snail.snailvr.model.VideoInfo;
import com.snail.snailvr.network.providers.downloads.DownloadManager;
import com.snail.snailvr.utils.DialogUtils;
import com.snail.snailvr.utils.IntentUtils;
import com.snail.snailvr.widget.ThumbView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 发现页面
 * Created by snail on 16/6/17.
 */

public class DiscoverAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private static final int VIEW_TYPE_LIVE = 0, VIEW_TYPE_NORMAL = 1;
    private static final int HEADER_COUNT = 1;
    private static final int DISCOVER_TYPE_LIVE = 1, DISCOVER_TYPE_VIDEO = 2;

    private LayoutInflater inflater;
    private Context mContext;
    private List<VideoInfo> mInfos;

    public DiscoverAdapter(Context context, List<VideoInfo> infos) {
        this.mContext = context;
        mInfos = infos;
        inflater = LayoutInflater.from(VRApp.getContext());
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_LIVE:
                View liveBanner = inflater.inflate(R.layout.discover_layout_live, parent, false);
                return new LiveHolder(liveBanner);
            default:
                return new DiscoverViewHolder(inflater.inflate(R.layout.discover_layout_normal, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final VideoInfo item = getItem(position);
        if(item!=null) {
            if (holder.getItemViewType() == VIEW_TYPE_LIVE) {
                LiveHolder liveHolder = (LiveHolder) holder;
                liveHolder.mThumbView.setImageUrlAndReUse(item.getCPicUrl());
                liveHolder.mThumbView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(!TextUtils.isEmpty(item.getCVideoUrl())) {
                             if(DialogUtils.showJudgeNetDialog(null,new View.OnClickListener() {
                                 @Override
                                 public void onClick(View view) {
                                     IntentUtils.playVideo(mContext, item.getCVideoUrl(), item.getSName());
                                 }
                             }))
                                 return;
                              IntentUtils.playVideo(mContext, item.getCVideoUrl(), item.getSName());
                        }
                    }
                });
            } else if (holder.getItemViewType() == VIEW_TYPE_NORMAL) {
                int status = item.getDownloadState();
                DiscoverViewHolder discoverViewHolder = (DiscoverViewHolder) holder;
                updateUI(discoverViewHolder, item);
                discoverViewHolder.updateDownloadState(status,item);
            }
        }
    }

    @NonNull
    private DiscoverViewHolder updateUI(DiscoverViewHolder discoverViewHolder, VideoInfo item) {

        discoverViewHolder.mThumbView.setImageUrlAndReUse(item.getCPicUrl());
        discoverViewHolder.mTitle.setText(item.getSName());
        discoverViewHolder.mVideoSize.setText(item.getCFileSize());
        discoverViewHolder.mUpdataTime.setText(item.getSDate());
        discoverViewHolder.mDuration.setText(item.getSVideoLength());

        return discoverViewHolder;
    }

    @Override
    public int getItemViewType(int position) {
        VideoInfo item = getItem(position);
        if (item != null && item.getcType() == DISCOVER_TYPE_LIVE)
            return VIEW_TYPE_LIVE;
        return VIEW_TYPE_NORMAL;
    }

    @Override
    public int getItemCount() {
        return mInfos.size();
    }

    private VideoInfo getItem(int position) {
        if (position < mInfos.size())
            return mInfos.get(position);
        return null;
    }



    class DiscoverViewHolder extends BaseDownloadViewHolder {
        @Bind(R.id.video_banner)
        ThumbView mThumbView;
        @Bind(R.id.video_title)
        TextView mTitle;
        @Bind(R.id.video_update_time)
        TextView mUpdataTime;
        @Bind(R.id.video_duration)
        TextView mDuration;
        @Bind(R.id.video_size)
        TextView mVideoSize;

        View itemView;

        public DiscoverViewHolder(View itemView) {
            super(mContext,itemView);
            this.itemView = itemView;
            mThumbView.setImagePlaceHolder(R.drawable.ic_discovery_img_default);
        }

        @Override
        void onSuccess(VideoInfo videoInfo) {
            //下载完成
            videoInfo.lintToMeida(mContext, videoInfo.getVideoPath());
            mDownloadStatus.setVisibility(View.INVISIBLE);
            mProgressBar.setVisibility(View.GONE);
            mDownloadStatus.setImageResource(R.drawable.ic_discovery_finish);
        }

        @Override
        void onPause(VideoInfo videoInfo) {
            showDonwloadProgressbar(videoInfo);
        }

        @Override
        void onRunning(VideoInfo videoInfo) {
            showDonwloadProgressbar(videoInfo);
        }

        @Override
        void onWait(VideoInfo videoInfo) {
            mDownloadStatus.setVisibility(View.VISIBLE);
            mProgressBar.setVisibility(View.GONE);
            mDownloadStatus.setOnClickListener(null);
            mDownloadStatus.setImageResource(R.drawable.ic_discovery_waiting);
        }

        @Override
        void onFail(VideoInfo videoInfo) {

        }

        @Override
        void onInit(VideoInfo videoInfo) {

            mDownloadStatus.setVisibility(View.VISIBLE);
            mProgressBar.setVisibility(View.GONE);
            mDownloadStatus.setImageResource(R.drawable.ic_discovery_download);
        }

        @Override
        void switchDownloadState(VideoInfo videoInfo,int viewId) {
            if(viewId==R.id.donut_download_progress) {
                switch (videoInfo.getDownloadState()) {
                    case DownloadManager.STATUS_PAUSED:
                        mProgressBar.setDownloadStatus(DownloadManager.STATUS_RUNNING);
                        videoInfo.setDownloadState(DownloadManager.STATUS_RUNNING);
                        break;
                    case DownloadManager.STATUS_RUNNING:
                        mProgressBar.setDownloadStatus(DownloadManager.STATUS_PAUSED);
                        videoInfo.setDownloadState(DownloadManager.STATUS_PAUSED);
                        break;
                }
                notifyDataSetChanged();
            }
        }


        private void showDonwloadProgressbar(VideoInfo videoInfo) {
            mProgressBar.setVisibility(View.VISIBLE);
            mProgressBar.setDownloadStatus(videoInfo.getDownloadState());
            mProgressBar.setProgress(videoInfo.getDownloadedPercent());
            mDownloadStatus.setVisibility(View.INVISIBLE);
        }
    }



    class LiveHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.live_banner)
        ThumbView mThumbView;

        public LiveHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            mThumbView.setImagePlaceHolder(R.drawable.ic_discovery_live_default);

        }
    }
}
